﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Restaurant.DTO;
using Restaurant.BUS;

namespace Restaurant
{
    public partial class frmChiTietKho : Form
    {
        private NguyenLieuService NLieuService = new NguyenLieuService();
        private ChiTietKhoService CTKhoService = new ChiTietKhoService();
        public frmChiTietKho()
        {
            InitializeComponent();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void frmChiTietKho_Load(object sender, EventArgs e)
        {

            LoadNCC();
            LoadLoaiNL();
            LoadKho();
            DataTable dbtab = NLieuService.LayNguyenLieu();
            LoadThongTinNL(dbtab);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            DataTable dbtab = NLieuService.LayNguyenLieu();
            LoadThongTinNL(dbtab);
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (lv_TatCaNL.FocusedItem != null)
            {
                if (txtToiDa.Text == "")
                {
                    MessageBox.Show("Nhập số lượng");
                    return;
                }
                if (txtToiThieu.Text == "")
                {
                    MessageBox.Show("Nhập số lượng");
                    return;
                }
                if (txtTon.Text == "")
                {
                    MessageBox.Show("Nhập số lượng");
                    return;
                }
                //Lay ma nguyen lieu
                int index = lv_TatCaNL.FocusedItem.Index;
                int maNL = int.Parse(lv_TatCaNL.Items[index].Text.ToString());
                // lay ma kho
                int maKho = int.Parse(cmbKho.SelectedValue.ToString());

                float ToiDa = float.Parse(txtToiDa.Text.ToString());
                float ToiThieu = float.Parse(txtToiThieu.Text.ToString());
                float LuongTon = float.Parse(txtTon.Text.ToString());
                if (ToiDa < ToiThieu)
                {
                    MessageBox.Show("Lượng tồn tối đa phải lớn hơn tối thiểu");
                    return;
                }
                // Tinh Trang: 0 het nguyen lieu, 1 sap het, 2 con
                int TinhTrang = 0;
                if (LuongTon > ToiThieu)
                    TinhTrang = 2;
                if (LuongTon <= ToiThieu && LuongTon > 0)
                    TinhTrang = 1;
                if (LuongTon <= 0)
                    TinhTrang = 0;

                if (!CTKhoService.ThemChiTietKho(maKho, maNL, ToiDa, ToiThieu, LuongTon, TinhTrang))
                {
                    MessageBox.Show("Thêm Không Thành Công");
                    
                    return;
                }
                DataTable dbtab = CTKhoService.LayChiTietKho(maKho);
                LoadThongTinNLKho(dbtab);
            }
            else
            {
                MessageBox.Show("Chưa chọn nguyên liệu");
                return;
            }
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            if (lv_NLKho.FocusedItem != null)
            {
                if (txtToiDa.Text == "")
                {
                    MessageBox.Show("Nhập số lượng");
                    return;
                }
                if (txtToiThieu.Text == "")
                {
                    MessageBox.Show("Nhập số lượng");
                    return;
                }
                if (txtTon.Text == "")
                {
                    MessageBox.Show("Nhập số lượng");
                    return;
                }
                //Lay ma nguyen lieu
                int index = lv_NLKho.FocusedItem.Index;
                int maNL = int.Parse(lv_NLKho.Items[index].Text.ToString());
                // lay ma kho
                int maKho = int.Parse(cmbKho.SelectedValue.ToString());

                float ToiDa = float.Parse(txtToiDa.Text.ToString());
                float ToiThieu = float.Parse(txtToiThieu.Text.ToString());
                float LuongTon = float.Parse(txtTon.Text.ToString());
                if (ToiDa < ToiThieu)
                {
                    MessageBox.Show("Lượng tồn tối đa phải lớn hơn tối thiểu");
                    return;
                }
                // Tinh Trang: 0 het nguyen lieu, 1 sap het, 2 con
                int TinhTrang = 0;
                if (LuongTon > ToiThieu)
                    TinhTrang = 2;
                if (LuongTon <= ToiThieu && LuongTon > 0)
                    TinhTrang = 1;
                if (LuongTon <= 0)
                    TinhTrang = 0;
                DataTable dbtab;
                //lost update
                if (!cb_fix.Checked && !cb_deadlock.Checked)
                {
                    if (!CTKhoService.SuaChiTietKho(maKho, maNL, ToiDa, ToiThieu, LuongTon, TinhTrang))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        dbtab = CTKhoService.LayChiTietKho(maKho);
                        LoadThongTinNLKho(dbtab);
                        return;
                    }
                }
                //dead lock
                if (!cb_fix.Checked && cb_deadlock.Checked)
                {
                    if (!CTKhoService.SuaChiTietKhoErro(maKho, maNL, ToiDa, ToiThieu, LuongTon, TinhTrang))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        dbtab = CTKhoService.LayChiTietKho(maKho);
                        LoadThongTinNLKho(dbtab);
                        return;
                    }
                }
                //fix dead lock
                if (cb_fix.Checked && cb_deadlock.Checked)
                {
                    if (!CTKhoService.SuaChiTietKhoFix(maKho, maNL, ToiDa, ToiThieu, LuongTon, TinhTrang))
                    {
                        MessageBox.Show("Sửa Không Thành Công");
                        dbtab = CTKhoService.LayChiTietKho(maKho);
                        LoadThongTinNLKho(dbtab);
                        return;
                    }
                }
                //MessageBox.Show("Sửa Thành Công");
                dbtab = CTKhoService.LayChiTietKho(maKho);
                LoadThongTinNLKho(dbtab);
            }
            else
            {
                MessageBox.Show("Chưa chọn nguyên liệu");
                return;
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (lv_NLKho.FocusedItem != null)
            {
                int index = lv_NLKho.FocusedItem.Index;
                int maNL = int.Parse(lv_NLKho.Items[index].Text.ToString());
                int maKho = int.Parse(cmbKho.SelectedValue.ToString());
                if (!CTKhoService.XoaChiTietKho(maKho,maNL))
                {
                    MessageBox.Show("Xóa Không Thành Công");
                    return;
                }
                DataTable dbtab = CTKhoService.LayChiTietKho(maKho);
                LoadThongTinNLKho(dbtab);
            }
            else
            {
                MessageBox.Show("Chưa chọn dữ liệu xóa");
                return;
            }
        }

        private void lv_TatCaNL_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void lv_NLKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                if (lv_NLKho.FocusedItem != null)
                {
                    int index = lv_NLKho.FocusedItem.Index;
                    int maNL = int.Parse(lv_NLKho.Items[index].Text.ToString());
                    int maKho = int.Parse(cmbKho.SelectedValue.ToString());
                    DataTable dbtab = CTKhoService.LayChiTietKhoNL(maKho,maNL);
                    DataRow dbrow = dbtab.Rows[0];
                    txtTon.Text = dbrow[4].ToString();
                    txtToiThieu.Text = dbrow[3].ToString();
                    txtToiDa.Text = dbrow[2].ToString();
                }
            }
            catch (Exception ex)
            {
                return;
                throw ex;
            }
        }

        private void cmbNCC_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maNCC = int.Parse(cmbNCC.SelectedValue.ToString());
            DataTable dbtab = NLieuService.LayNguyenLieuTheoNCC(maNCC);
            LoadThongTinNL(dbtab);
        }

        private void cmbLoaiNL_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maLoaiNL = int.Parse(cmbLoaiNL.SelectedValue.ToString());
            DataTable dbtab = NLieuService.LayNguyenLieuTheoLoai(maLoaiNL);
            LoadThongTinNL(dbtab);
        }

        private void cmbKho_SelectedIndexChanged(object sender, EventArgs e)
        {
            int maKho = int.Parse(cmbKho.SelectedValue.ToString());
            DataTable dbtab = CTKhoService.LayChiTietKho(maKho);
            LoadThongTinNLKho(dbtab);
        }
        private void LoadNCC()
        {
            NhaCungCapService NCC = new NhaCungCapService();
            cmbNCC.DisplayMember = "TenNCC";
            cmbNCC.ValueMember = "MaNCC";
            cmbNCC.DataSource = NCC.LoadTatCaNhaCungCap();
        }
        private void LoadLoaiNL()
        {
            LoaiNguyenLieuService LoaiNL = new LoaiNguyenLieuService();
            cmbLoaiNL.DisplayMember = "TenLoaiNL";
            cmbLoaiNL.ValueMember = "MaLoaiNL";
            cmbLoaiNL.DataSource = LoaiNL.LoadLoaiNguyenLieu();
        }
        private void LoadKho()
        {
            KhoService Kho = new KhoService();
            cmbKho.DisplayMember = "TenKho";
            cmbKho.ValueMember = "MaKho";
            cmbKho.DataSource = Kho.LayKho();
        }
        public void LoadThongTinNL(DataTable dbtab)
        {
            lv_TatCaNL.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow[0].ToString();
                liv.SubItems.Add(dbrow[1].ToString());
                liv.SubItems.Add(dbrow[2].ToString());
                liv.SubItems.Add(dbrow[3].ToString());
                liv.SubItems.Add(dbrow[4].ToString());
                lv_TatCaNL.Items.Add(liv);
            }
        }

        public void LoadThongTinNLKho(DataTable dbtab)
        {
            lv_NLKho.Items.Clear();
            for (int i = 0; i < dbtab.Rows.Count; i++)
            {
                DataRow dbrow = dbtab.Rows[i];
                ListViewItem liv = new ListViewItem();
                liv.Text = dbrow["MaNL"].ToString();
                liv.SubItems.Add(dbrow["TenNL"].ToString());
                liv.SubItems.Add(dbrow["TenNCC"].ToString());
                liv.SubItems.Add(dbrow["LuongTon"].ToString());
                liv.SubItems.Add(dbrow["SucChuaThapNhat"].ToString());
                liv.SubItems.Add(dbrow["TenDVTinh"].ToString());
                //Tinh Trang
                if(int.Parse(dbrow[5].ToString()) == 0)
                    liv.SubItems.Add("Hết NL");
                if (int.Parse(dbrow[5].ToString()) == 1)
                    liv.SubItems.Add("Sắp Hết NL");
                if (int.Parse(dbrow[5].ToString()) == 2)
                    liv.SubItems.Add("Còn NL");
                lv_NLKho.Items.Add(liv);
            }
        }
    }
}
